<?php

class Block extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'pages':
	 * @var string $alias
	 * @var string $title
	 * @var string $text
	 */
	public $preid = 'block';

	/**
	 * Returns the static model of the specified AR class.
	 * @return Page the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'blocks';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('preid, alias, title, text', 'required'),
			array('alias', 'unique'),
			array('alias', 'length', 'max'=>25),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, text', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'alias' => 'Метка',
			'title' => 'Название',
			'text' => 'Текст',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($criteria = null)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		if($criteria == null)
			$criteria=new CDbCriteria;

		$criteria->compare('alias',$this->alias, true);

		$criteria->compare('title',$this->title,true);

		$criteria->compare('text',$this->text,true);

		return new CActiveDataProvider('Block', array(
			'criteria'=>$criteria,
		));
	}
	
	public function beforeValidate(){
		Yii::import('ext.markdown.*');
		$md = new EMarkdown;
		$this->text = $md->transform($this->text);

		return parent::beforeValidate();
	}
	
}